'use strict';
module.exports = {
	/**
	 * 获取推荐课程分页
	 * @url client/course/pub/recommend_course 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid, pageIndex, pageSize, type, category_id, is_rec, keyword } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
		let fieldJson = {}
		let dbName = "course";
		let whereJson = {
			is_rec: is_rec, // 推荐课程
			category_parent_id: category_id, // 分类ID
			title: new RegExp(keyword) // 搜索条件
		}
		// 根据类型传入条件,2传子
		if (type == 2) {
			whereJson = {
				is_rec: is_rec,
				category_id: category_id
			}
		}

		let foreignDB = []
		if (uid != null) {
			foreignDB = [{
				dbName: "user_goods",
				localKey: "_id",
				foreignKey: "goods_id",
				whereJson: { user_id: uid },
				as: "isBuy",
				limit: 1,
			}]
		}
		res = await vk.baseDao.selects({
			dbName: dbName, // 表名
			getMain: false, // 是否只返回rows数据
			pageIndex: pageIndex, // 当前第几页
			pageSize: pageSize, // 每页条数
			foreignDB,
			sortArr: [{ "name": "_add_time", "type": "desc" }],
			// 条件
			whereJson: whereJson
		});

		if (uid != null) {
			await vk.baseDao.updateById({
				dbName: "uni-id-users",
				id: uid,
				dataJson: { last_check_home: new Date().getTime() }
			});
		}
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
